{template 'common/header'}
<title>支付订单</title>
<style type="text/css">
    body {margin:0px; background:#efefef; font-family: '楷体' , '宋体' , Tahoma, Helvetica, Arial, "\5b8b\4f53", sans-serif; -moz-appearance:none;}
    .order_main {height:auto; border-bottom:1px solid #f0f0f0; border-top:1px solid #f0f0f0; background:#fff;margin-top:10px;}
    .order_main .line {height:44px; margin:0 5px; border-bottom:1px solid #f0f0f0; line-height:44px; overflow: hidden;}
    .order_main .line .label { float:left; width:80px; line-height: 44px;color: #666; height: 44px;  padding: 0;}
    .order_main .line .info { float:right; width:100%; margin-left:-85px;text-align: right;overflow:hidden;height:44px;}
    .order_main .line .info .inner { color:#666;margin-left:85px;}
    .order_main .tip { color:#666; line-height:20px;padding:5px;font-size:13px; }

    .order_main .line .nav {height:22px; width:40px; background:#ccc; margin:10px 0px; float:right; border-radius:40px;}
    .order_main .line .on {background:#4ad966;}
    .order_main .line .nav nav {height:20px; width:20px; background:#fff; margin:1px; border-radius:20px;}
    .order_main .line .nav .on {margin-left:19px;}

    .order_sub1 {height:44px; margin:14px 5px; background:#31cd00; border-radius:4px; text-align:center; font-size:16px; line-height:44px; color:#fff;}
    .order_sub2 {height:44px; margin:14px 5px; background:#f49c06; border-radius:4px; text-align:center; font-size:16px; line-height:44px; color:#fff;}
    .order_sub3 {height:44px; margin:14px 5px;background:#e2cb04; border-radius:4px; text-align:center; font-size:16px; line-height:44px; color:#fff;}
    .order_sub4 {height:44px; margin:14px 5px; background:#18c0f7; border-radius:4px; text-align:center; font-size:16px; line-height:44px; color:#fff;}

    .order_main1 {/*height:30px;*/padding:10px;border-bottom:1px solid #f0f0f0; border-top:1px solid #f0f0f0; background:#fff;text-align:center;margin-top:10px; }
    .order_sub5 {height:30px; width:35%;padding:5px 10px 5px 10px; border:1px solid #ccc; border-radius:4px; text-align:center; font-size:16px; line-height:30px; color:#333; }
    .order_sub6 {height:44px; margin:14px 5px; background:#07c4d0; border-radius:4px; text-align:center; font-size:16px; line-height:44px; color:#fff;}

    .order_subc {height:44px; margin:14px 5px; background:#31cd00; border-radius:4px; text-align:center; font-size:16px; line-height:44px; color:#fff;}
    #core_alert .tips{    width: auto;  position: absolute;  left: 50%;  right: 50%;}
    #core_alert .tips .sub{margin-top:-30px}
</style>

<div id='container'></div>
<script id='tpl_order_info' type='text/html'>
    <input type='hidden' id='orderid' value="<%order.id%>"/>
    <div class="page_topbar">
        <a href="javascript:;" class="back" onclick="history.back()"><i class="fa fa-angle-left"></i></a>
        <div class="title">支付订单</div>
    </div>
    <div class="order_main" >
        <div class="line"><div class="label">订单编号</div><div class="info"><div class="inner"><%order.ordersn%></div></div></div>
        <div class="line"><div class="label">支付金额</div><div class="info"><div class="inner"><div style='color:#2b2b2b'>￥<span id="orderprice" price="<%order.price%>"><%order.price%></span>元</div></div></div></div>
    </div>
    <%if order.price>0%>
    <%if wechat.success%><div class="button order_sub1">微信支付</div><%/if%>
    <%if alipay.success%><div class="button order_sub2" >支付宝支付</div><%/if%>

    <%if credit.success %>
    <div class="button order_sub3">余额支付(当前余额:<%credit.current%>)</div>
    <input type="hidden" id="credit" value="<%credit.current%>" />
    <%if credit.current<=0%>
    <div class="button order_sub4" onclick="location.href='{php echo $this->createMobileUrl('member/recharge')}&returnurl=<%returnurl%>'">账户充值</div>
    <%/if%>
    <%/if%>

    <%/if%>

    <div class="button order_subc"  <%if order.price>0%>style="display:none"<%/if%>>确认支付</div>
    <%if cash.success%><div class="button order_sub6" >货到付款</div><%/if%>
</script>

<script id='tpl_order_pay' type='text/html'>
    <div class="page_topbar">
        <div class="title">支付成功</div>
    </div>
    <img src="../addons/ewei_shop/template/mobile/default/static/images/pay_ok.png" style="width:100%;" />
    <div class="order_main" >
        <%if address%>
        <div class="line"><div class="label">收货人</div><div class="info"><div class='inner'><%address.realname%> <%address.mobile%></div></div></div>
        <div class="line"><div class="label">收货地址</div><div class="info"><div class='inner'><%address.address%></div></div></div>
        <%/if%>
        <%if carrier%>
        <%if order.isverify=='1'%>
        <div class="line"><div class="label">联系人</div><div class="info"><div class='inner'><%carrier.carrier_realname%></div></div></div>
        <div class="line"><div class="label">联系电话</div><div class="info"><div class='inner'><%carrier.carrier_mobile%></div></div></div>
        <%else%>
        <div class="line"><div class="label">自提地点</div><div class="info"><div class='inner'><%carrier.address%></div></div></div>
        <div class="line"><div class="label">自提联系人</div><div class="info"><div class='inner'><%carrier.realname%> <%carrier.mobile%></div></div></div>
        <%/if%>
        <%/if%>
        <div class="line"><div class="label">实付款</div><div class="info"><div class='inner'><span style='color:#ff6600'>￥<%order.price%>元</span></div></div></div>
    </div>
    <div class="order_main1" >
        <span class="order_sub5" onclick="location.href='{php echo $this->createMobileUrl('order/detail')}&id=<%order.id%>'">订单详情</span>
        <span class="order_sub5" onclick="location.href='{php echo $this->createMobileUrl('shop')}'">返回首页</span>
    </div>
</script>

<script id='tpl_order_cash' type='text/html'>
    <div class="page_topbar">
        <div class="title">订单提交成功</div>
    </div>
    <img src="../addons/ewei_shop/template/mobile/default/static/images/pay_cash.png" style="width:100%;" />
    <div class="order_main" >
        <%if address%>
        <div class="line"><div class="label">收货人</div><div class="info"><div class='inner'><%address.realname%> <%address.mobile%></div></div></div>
        <div class="line"><div class="label">收货地址</div><div class="info"><div class='inner'><%address.address%></div></div></div>
        <%/if%>
        <%if carrier%>
        <%if order.isverify=='1'%>
        <div class="line"><div class="label">联系人</div><div class="info"><div class='inner'><%carrier.carrier_realname%></div></div></div>
        <div class="line"><div class="label">联系电话</div><div class="info"><div class='inner'><%carrier.carrier_mobile%></div></div></div>
        <%else%>
        <div class="line"><div class="label">自提地点</div><div class="info"><div class='inner'><%carrier.address%></div></div></div>
        <div class="line"><div class="label">自提联系人</div><div class="info"><div class='inner'><%carrier.realname%> <%carrier.mobile%></div></div></div>
        <%/if%>
        <%/if%>
        <div class="line"><div class="label">需到付</div><div class="info"><div class='inner'><span style='color:#ff6600'>￥<%order.price%>元</span></div></div></div>
    </div>
    <div class="order_main1" >
        <span class="order_sub5" onclick="location.href='{php echo $this->createMobileUrl('order/detail')}&id=<%order.id%>'">订单详情</span>
        <span class="order_sub5" onclick="location.href='{php echo $this->createMobileUrl('shop')}'">返回首页</span>
    </div>
</script>


<style type="text/css">
    .pop-dialog {position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 999;display: none;}
    .pop-dialog .bg {width: 100%;height: 100%;position: absolute;top: 0;left: 0;opacity: 0.9;background-color: #000;}
    .pop-dialog .body {position: absolute;top: 0;right: 10%;width: 80%;padding-top: 46px;color: #fff;}
    .pop-dialog .body > p {line-height: 1.4;font-size: 14px;padding: 6px 0;}
    .pop-dialog .body img.icon {width: 36px;margin: 0 5px;display: inline-block;}
    .pop-dialog .body img.arrow {position: absolute;right: -10%;top: 0;display: block;margin-right: -13px;width: 70px;}
    i.icon {padding: 4px 12px;margin: 0 3px;border-radius: 3px;background-color: #3b3b3b;font-size: 14px;}
    a.btn {background-color: #3b3b3b;color: #fff;margin-top:15px;display: block;margin: 0 auto;padding: 10px 20px;border-radius: 3px;text-align: center;text-decoration: none;font-size: 16px;}
</style>

<div class="pop-dialog" onclick="$(this).hide();">
    <div class="bg"></div>
    <div class="body">
        <img class="arrow" src="../addons/ewei_shop/template/mobile/default/static/images/arrow.png">
        <p>1. 点击右上角 <i class="fa fa-ellipsis-h"></i> 或 <i class="fa fa-ellipsis-v"></i></p>
        <p>2. 由于在微信中无法打开支付宝付款页面，请选择在浏览器中打开，即可访问购买页面</p>

        <p>3. 付款成功后，请返回到这里点击下面的返回按钮</p>
        <a href="{php echo $this->createMobileUrl('order/detail',array('id'=>$_GPC['orderid']))}" class="btn">返回</a>&nbsp;

    </div>
</div>

<script language="javascript">
    function isWeiXin(){
        var ua = window.navigator.userAgent.toLowerCase();
        if(ua.match(/MicroMessenger/i) == 'micromessenger'){
            return true;
        }else{
            return false;
        }
    }

    require(['tpl', 'core'], function(tpl, core) {
        core.json('order/pay',{orderid:'{$_GPC['orderid']}',openid:"{$openid}"},function(json){
            var result = json.result;
            if(json.status==-1){
                location.href = core.getUrl('order/detail',{id:"{$_GPC['orderid']}"});
                return;
            }
            if(json.status!=1){
                core.message(result,"{php echo $this->createMobileUrl('order/detail',array('id'=>$_GPC['orderid']))}",'error');
                return;
            }
            $('#container').html(tpl('tpl_order_info',result));

            if(result.alipay.success){

                $('.order_sub2').click(function(){
                    if( isWeiXin() ) {
                        $('.pop-dialog').show();
                        return;
                    }
                    var deduct = ($('#deductmoney').length>0 &&$('#deductmoney').attr('on')=='1' )?1:0 ;
                    core.json('order/pay', {op: 'pay',type: 'alipay', orderid:'{$_GPC['orderid']}',openid:"{$openid}",deduct:deduct}, function (rjson) {
                        if(rjson.status!=1){
                            $('.button').removeAttr('submitting');
                            core.tip.show(rjson.result);
                            return;
                        }
                        location.href =  rjson.result.alipay.url;
                    },true,true);
                })
            }
            if(result.credit.success){

                $(".order_sub3").click(function(){
                    if($(this).attr('submitting')=='1'){
                        return;
                    }
                    core.tip.confirm('确认要立即付款?',function(){
                        $('.button').attr('submitting',1);
                        core.json('order/pay',{
                            op:'complete',
                            orderid:'{$_GPC['orderid']}',
                            type:'credit'
                        },function(pay_json){
                            if(pay_json.status==1){
                                $('#container').html(tpl('tpl_order_pay',pay_json.result));
                                return;
                            }
                            core.tip.show(pay_json.result);
                            $('.button').removeAttr('submitting');
                        },true,true);
                    });
                });
            }

            if(result.cash.success){
                $(".order_sub6").click(function(){
                    if($(this).attr('submitting')=='1'){
                        return;
                    }
                    core.tip.confirm('确认要货到付款?',function(){
                        $('.button').attr('submitting',1);
                        core.json('order/pay',{
                            op:'complete',
                            orderid:'{$_GPC['orderid']}',
                            type:'cash'
                        },function(pay_json){
                            if(pay_json.status==2){
                                $('#container').html(tpl('tpl_order_cash',pay_json.result));
                                return;
                            }
                            core.tip.show(pay_json.result);
                            $('.button').removeAttr('submitting');
                        },true,true);
                    });})
            }

            if(result.wechat.success){
                $('.order_sub1').click(function(){
                    if($(this).attr('submitting')=='1'){
                        return;
                    }
                    $('.button').attr('submitting',1);
                    var deduct = ($('#deductmoney').length>0 &&$('#deductmoney').attr('on')=='1' )?1:0 ;
                    core.json('order/pay', {op: 'pay',type: 'weixin', orderid:'{$_GPC['orderid']}',deduct:deduct}, function (rjson) {
                        if(rjson.status!=1){
                            $('.button').removeAttr('submitting');
                            core.tip.show(rjson.result);
                            return;
                        }

                        var wechat = rjson.result.wechat;

                        require(['http://res.wx.qq.com/open/js/jweixin-1.0.0.js'],function(wx){
                            jssdkconfig = {php echo json_encode($_W['account']['jssdkconfig']);} || { jsApiList:[] };
                            jssdkconfig.debug = false;
                            jssdkconfig.jsApiList = ['checkJsApi','chooseWXPay'];
                            wx.config(jssdkconfig);
                            wx.ready(function () {
                                $('.button').removeAttr('submitting');
                                var appid = wechat.appid?wechat.appid:wechat.appId;
                                wx.chooseWXPay({
                                    'appId' :  appid,
                                    'timestamp': wechat.timeStamp,
                                    'nonceStr' : wechat.nonceStr,
                                    'package' :  wechat.package,
                                    'signType' : wechat.signType,
                                    'paySign' : wechat.paySign,
                                    success: function (res) {

                                        core.json('order/pay',{
                                            op:'complete',
                                            orderid:'{$_GPC['orderid']}',
                                            type:'weixin',deduct:deduct
                                        },function(pay_json){
                                            if(pay_json.status==1){
                                                $('#container').html(tpl('tpl_order_pay',pay_json.result));
                                                return;
                                            }
                                            core.tip.show(pay_json.result);
                                            $('.button').removeAttr('submitting');
                                        },true,true);

                                    },fail:function(res){
                                        alert(res.errMsg);
                                    }
                                });
                            });
                        });
                    },true,true);
                });
            }
            $('.order_subc').click(function(){
                core.tip.confirm('确认要立即付款?',function(){
                    $('.button').attr('submitting',1);
                    core.json('order/pay',{
                        op:'complete',
                        orderid:'{$_GPC['orderid']}',
                        type:'credit'
                    },function(pay_json){
                        if(pay_json.status==1){
                            $('#container').html(tpl('tpl_order_pay',pay_json.result));
                            return;
                        }
                        core.tip.show(pay_json.result);
                        $('.button').removeAttr('submitting');
                    },true,true);
                })
            });


        },true)
    });

</script>
{template 'common/footer'}